package com.richpencil.config.datasource;

import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;
import java.util.Properties;

/*
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactoryFL",
        transactionManagerRef = "transactionManagerFL",
        basePackages = {DataSourceFactoryFL.PACKAGE})
@MapperScan(basePackages = DataSourceFactoryFL.PACKAGE_MYBATIS, sqlSessionTemplateRef = "sqlSessionTemplateFL")
*/
public class DataSourceFactoryFL {

    static final String PACKAGE = "com.richpencil.flmodules";
    static final String PACKAGE_MYBATIS = "com.richpencil.flmodules.mapper";
    static final String MAPPER_LOCATION = "classpath:mapper/fl/*.xml";


    @Autowired
    private HibernateProperties hibernateProperties;
    @Resource(name = "DataSourceFL")
    private DataSource DataSourceFL;
    @Autowired
    private JpaProperties jpaProperties;
    @Bean(name = "entityManagerFactoryFL")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryFL(EntityManagerFactoryBuilder builder) {

        jpaProperties.getProperties().put("hibernate.dialect","org.hibernate.dialect.SQLServer2008Dialect");
        Map<String, Object> properties = hibernateProperties.determineHibernateProperties(
                jpaProperties.getProperties(), new HibernateSettings());
        return builder.dataSource(DataSourceFL).properties(properties)
            .packages(DataSourceFactoryFL.PACKAGE).persistenceUnit("flPersistenceUnit")
            .build();
    }

    @Bean(name = "entityManagerFL")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryFL(builder).getObject().createEntityManager();
    }






    @Bean(name = "transactionManagerFL")
    public PlatformTransactionManager transactionManagerFL(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryFL(builder).getObject());
    }




/*

    @Bean(name = "transactionManagerFLSQL")
    public DataSourceTransactionManager TransactionManagerPirmarySQL() {
        return new DataSourceTransactionManager(DataSourceFL);
    }
*/

    @Bean(name = "sqlSessionFactoryFL")
    public SqlSessionFactory SqlSessionFactoryK3(@Qualifier("DataSourceFL") DataSource flDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(flDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(DataSourceFactoryFL.MAPPER_LOCATION));
        //分页插件
        Interceptor interceptor = new PageInterceptor();
        Properties properties = new Properties();
        //数据库
        properties.setProperty("helperDialect", "sqlserver");
        //是否将参数offset作为PageNum使用
        //properties.setProperty("offsetAsPageNum", "false");
        //是否进行count查询
        properties.setProperty("rowBoundsWithCount", "true");
        //是否分页合理化
        properties.setProperty("reasonable", "false");
        properties.setProperty("params","count=countSql");
        interceptor.setProperties(properties);
        sessionFactory.setPlugins(new Interceptor[] {interceptor});

        return sessionFactory.getObject();
    }

    @Bean(name = "sqlSessionTemplateFL")
    public SqlSessionTemplate sqlSessionTemplateOne(@Qualifier("sqlSessionFactoryFL") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }


}
